package com.ysd.upload.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ysd.upload.common.JSONResult;
import com.ysd.upload.param.in.UploadFileRequestType;
import com.ysd.upload.param.out.FileUploadInfo;
import com.ysd.upload.param.out.UploadFileResponseType;
import com.ysd.upload.service.impl.UploadServiceImpl;

@RestController
@RequestMapping("/fileupload")
public class FileUploadController {

	@Autowired
	private UploadServiceImpl uploadService;

	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public JSONResult<String> handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
		return uploadService.aliyunUploadFile(file);
	}
	
	@RequestMapping(value = "/uploadFile01", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public JSONResult<String> handleFileUpload03(HttpServletRequest request) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
         // 获取上传的文件
         MultipartFile multiFile = multipartRequest.getFile("file");
         return uploadService.aliyunUploadFile(multiFile);
	}
	
	@RequestMapping(value = "/test", method = RequestMethod.POST)
	public UploadFileResponseType handleFileUploadTest(UploadFileRequestType request) {
		UploadFileResponseType response = new UploadFileResponseType();
		List<FileUploadInfo> fileLis = new ArrayList<FileUploadInfo>();
		FileUploadInfo file1 = new FileUploadInfo();
		file1.setFileName("测试文件1.txt");
		FileUploadInfo file2 = new FileUploadInfo();
		file2.setFileName("测试文件2.txt");
		fileLis.add(file1);
		fileLis.add(file2);
		response.setFileLis(fileLis);
        return response;
	}
	

}
